package cn.itcast.bos.web.action.base;



import java.util.Collection;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.IFixedAreaService;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class FixedAreaAction extends BaseAction<FixedArea> {
	
	@Autowired
	private IFixedAreaService fixedAreaService;
	
	@Action(value="fixed_save",results={@Result(name="success",location="./pages/base/fixed_area.html",type="redirect")})
	public String save(){
		System.err.println(model);
		fixedAreaService.save(model);
		return SUCCESS;
	}
	
	@Action(value="fixed_queryPage",results={@Result(name="success",type="json")})
	public String findPageData(){
		Pageable pageable = new PageRequest(page-1, rows);
		
		Specification<FixedArea> specification= new Specification<FixedArea>() {
			
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate=null;
				if(StringUtils.isNotBlank(model.getId())){
					Predicate p1 = cb.equal(root.get("id"), model.getId());
					predicate=cb.and(p1);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					Predicate p2 = cb.equal(root.get("company"), model.getCompany());
					predicate=cb.and(p2);
				}
				if(StringUtils.isNotBlank(model.getId())){
					Predicate p3 = cb.equal(root.get("id"), model.getId());
					predicate=cb.and(p3);
				}
				 return null;
			}
		};
		Page<FixedArea> pageData=fixedAreaService.findPageData(specification,pageable);
		pushPageDataToValuestack(pageData);
		return SUCCESS;
	}
	//查询未关联的客户
	@Action(value="find_noassociationcustomer",results={@Result(name="success",type="json")})
	public String findNoAssociationCustomer(){
	
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
	
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	

	//查询已关联客户
	//find_associationfixedareacustomer
	@Action(value="find_associationfixedareacustomer",results={@Result(name="success",type="json")})
	public String findAssociationFixedAreaCustomer(){
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"+model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
	
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	private String[] customerIds;
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	//查询已关联客户
	@Action(value="fixedarea_associationcustomertofixedarea",
			results={@Result(name="success",location="./pages/base/fixed_area.html"
			,type="redirect")})
	public String associationFixedAreaCustomer(){
		for (String str : customerIds) {
			System.out.print(str+"==");
		}
		
		String customerIdStr = StringUtils.join(customerIds,",");
		
		 WebClient.create("http://localhost:9002/crm_management/services/"
				+ "customerService/associationcustomerstofixedarea?customerIdStr="
				+customerIdStr+"&fixedAreaId="+model.getId())
				.put(null);
		return SUCCESS;
	}
	private Integer courierId;
	private Integer takeTimeId;
	
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	@Action(value="fixedArea_associationCourierToFixedArea",
			results={@Result(name="success",location="./pages/base/fixed_area.html"
			,type="redirect")})
	public String associationCourierToFixedArea(){
		fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
		
		return SUCCESS;
	}
}
